package Dao;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import Model1.sellorder;

public class sellorderDao {
		
		
	private static Connection conn;
	private static PreparedStatement ps;
	private static ResultSet rs;
		
		
	public static Boolean Insert(sellorder order) {
		if(order==null)
			return false;
		conn = DatabaseConn.getConn();
		String sql = "insert into sellorder(orderid,selldate,total) values(?,?,?)";
		try {
				ps = conn.prepareStatement(sql);
				ps.setString(1, order.getOrderId());
				ps.setString(2, order.getSelldate());
				ps.setDouble(3, order.getTotal());
				int i = ps.executeUpdate();
				DatabaseConn.close(conn, ps, rs);
				return i==1;
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return false;
		}
		
		public static sellorder SearchById(String orderId) {
			if(orderId.length()==0)
				return null;
			conn = DatabaseConn.getConn();
			sellorder order = null;
			String sql = "select * from sellorder where orderid like '"+orderId+"'";
			try {
				ps = conn.prepareStatement(sql);
				rs = ps.executeQuery();
				while(rs.next()) {
					order = new sellorder(rs.getString("orderid"),rs.getString("selldate"),rs.getDouble("total"));
				}
				DatabaseConn.close(conn, ps, rs);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return order;
		}
		
		public static Boolean Delete(int orderId) {
			if(orderId<=0)
				return false;
			conn = DatabaseConn.getConn();
			String sql = "delete from orders where id like "+orderId;
			try {
				ps = conn.prepareStatement(sql);
				int i =ps.executeUpdate();
				DatabaseConn.close(conn, ps, rs);
				return i==1;
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return false;
		}
		
		public static Boolean Update(sellorder order) {
			conn = DatabaseConn.getConn();
			String sql = "update sellorder set selldate=?,total=? where orderid=?";
			try {
				ps = conn.prepareStatement(sql);
				ps.setString(1, order.getSelldate());
				ps.setDouble(2, order.getTotal());
				ps.setString(3, order.getOrderId());
				int i = ps.executeUpdate();
				DatabaseConn.close(conn, ps, rs);
				return i==1;
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return false;
		}
		
		public static List<sellorder> findAll() {
			conn = DatabaseConn.getConn();
			List<sellorder> order=new ArrayList<>();
			String sql = "select * from sellorder";
			try {
				ps = conn.prepareStatement(sql);
				rs = ps.executeQuery();
				while(rs.next()) {
					order.add(new sellorder(rs.getString("orderid"),rs.getString("selldate"),rs.getDouble("total")));
				}
				DatabaseConn.close(conn, ps, rs);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return order;
		}
		
		public static String SearchMaxId() {
			
			conn = DatabaseConn.getConn();
			sellorder orders = null;
			String orderId="";
			String sql = "select MAX(orderid) from sellorder";
			try {
				ps = conn.prepareStatement(sql);
				rs = ps.executeQuery();
				while(rs.next()) {
					orderId=rs.getString("MAX(orderid)");
				}
				DatabaseConn.close(conn, ps, rs);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return orderId;
		}
		
		public static void alterPrice(float price,String orderId) {
			conn = DatabaseConn.getConn();
			String sql = "update sellorder set total ="+price+" where orderid='"+orderId+"'";
		}
}
